package com.huanming.dsjc.modular.goods.controller;

import com.huanming.dsjc.common.constant.factory.ConstantFactory;
import com.huanming.dsjc.common.controller.BaseController;
import com.huanming.dsjc.common.exception.BizExceptionEnum;
import com.huanming.dsjc.common.exception.BussinessException;
import com.huanming.dsjc.common.node.ZTreeNode;
import com.huanming.dsjc.common.persistence.model.Dept;
import com.huanming.dsjc.core.log.LogObjectHolder;
import com.huanming.dsjc.core.util.ToolUtil;
import com.huanming.dsjc.modular.goods.dao.GoodsCategoryMapper;
import com.huanming.dsjc.modular.goods.entity.GoodsCategory;
import com.huanming.dsjc.modular.goods.service.IGoodsCategoryService;
import com.huanming.dsjc.modular.goods.warpper.GoodsCategoryWarpper;
import com.huanming.dsjc.modular.system.warpper.DeptWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
import java.util.Map;

/**
 * 商品分类控制器
 *
 * @author xufei
 * @Date 2018-01-17 15:15:25
 */
@Controller
@RequestMapping("/goodscategory")
public class GoodsCategoryController extends BaseController {

    private String PREFIX = "/goods/goodscategory/";

    @Autowired
    private GoodsCategoryMapper goodsCategoryDao;

    @Autowired
    IGoodsCategoryService iGoodsCategoryService;

    /**
     * 跳转到商品分类首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "goodscategory.html";
    }

    /**
     * 跳转到添加商品分类
     */
    @RequestMapping("/goodscategory_add")
    public String GoodsCategoryAdd() {
        return PREFIX + "goodscategory_add.html";
    }

    /**
     * 跳转到修改商品分类
     */
    @RequestMapping("/goodscategory_update/{goodsCategoryId}")
    public String GoodsCategoryUpdate(@PathVariable Integer goodsCategoryId, Model model) {
        GoodsCategory goodsCategory = goodsCategoryDao.selectById(goodsCategoryId);
        model.addAttribute(goodsCategory);
        model.addAttribute("pName", ConstantFactory.me().getCategory(goodsCategory.getPid()));
        LogObjectHolder.me().set(goodsCategory);
        return PREFIX + "goodscategory_edit.html";
    }

    /**
     * 获取部门的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.goodsCategoryDao.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    /**
     * 获取商品分类列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.goodsCategoryDao.list(condition);
        return super.warpObject(new GoodsCategoryWarpper(list));
    }

    /**
     * 新增商品分类
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(GoodsCategory goodsCategory) {
        if (ToolUtil.isEmpty(goodsCategory)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        goodsCategorySetPids(goodsCategory);
        goodsCategoryDao.insert(goodsCategory);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除商品分类
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer categoryId) {
        //缓存被删除的部门名称
        LogObjectHolder.me().set(ConstantFactory.me().getDeptName(categoryId));

        iGoodsCategoryService.deleteCategory(categoryId);
        return SUCCESS_TIP;
    }


    /**
     * 修改商品分类
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(GoodsCategory goodsCategory) {
        if (ToolUtil.isEmpty(goodsCategory) || goodsCategory.getId() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        goodsCategorySetPids(goodsCategory);
        goodsCategoryDao.updateById(goodsCategory);
        return super.SUCCESS_TIP;
    }

    /**
     * 商品分类详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }

    private void goodsCategorySetPids(GoodsCategory goodsCategory) {
        if (ToolUtil.isEmpty(goodsCategory.getPid()) || goodsCategory.getPid().equals(0)) {
            goodsCategory.setPid(0);
            goodsCategory.setPids("[0],");
        } else {
            int pid = goodsCategory.getPid();
            GoodsCategory temp = goodsCategoryDao.selectById(pid);
            String pids = temp.getPids();
            goodsCategory.setPid(pid);
            goodsCategory.setPids(pids + "[" + pid + "],");
        }
    }
}
